package com.teamsun.mqtt.handle;

import java.nio.CharBuffer;
import java.nio.charset.Charset;

import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class TelnetHandle extends ChannelInboundHandlerAdapter {

	StringBuilder commond=new StringBuilder();
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {

		String str = "hello word";

		String string = (String) msg;
		if (!string.endsWith(":"))
			ctx.fireChannelRead(msg);
		else if(string.endsWith("\n")){
			commond.append(string.trim());
			
			CharBuffer charBuffer=CharBuffer.wrap((commond.toString().toCharArray()));
			ctx.writeAndFlush(
					ByteBufUtil.encodeString(ctx.alloc(), charBuffer,Charset.forName("utf-8")));
			
		}
		else {
			commond.append(str);
		}

	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		super.channelReadComplete(ctx);
	}

}
